package com.woniu.project.consumer;

import com.woniu.project.web.common.MessageCommon;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/** 消费类
 * @Author: Klaus
 * @Date: 2020/9/23 20:49
 */
@Component
//RocketMQMessageListener对消息进行筛选   topic 主题  consumerGroup组名
//selectorExpression标签名   consumeMode = ConsumeMode.ORDERLY  保证单线程
@RocketMQMessageListener(topic = "message",consumerGroup = "my-producer"/*,consumeMode = ConsumeMode.ORDERLY/*,selectorExpression = "tag1"*/)
public class MyConsumer implements RocketMQListener<String> {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public void onMessage(String ManagementPhone) {
        //随机生成一个六位数 作为短信验证码
        String random=(int)((Math.random()*9+1)*100000)+"";
        //将验证码存入redis中进行保存(设置5分钟过期)
//        redisTemplate.opsForValue().set("messageCode:"+ManagementPhone+":",random,60 * 5, TimeUnit.SECONDS);
        System.out.println("短信验证码为+++："+random);
        redisTemplate.opsForValue().set("messageCode:"+ManagementPhone+":",random);
        System.out.println("短信验证码为---："+random);
        //调用发送短信的方法
        MessageCommon.SendSms(ManagementPhone,random);
    }

}
